//
//  ViewController1.m
//  Wedding fantastic
//
//  Created by ZLM on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TaoCanViewController.h"

@interface TaoCanViewController ()

@end

@implementation TaoCanViewController
-(void)dealloc{
    [_myButtonName release];
    [_strUrl release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    
//    if ([self.buttonName isEqualToString:@"婚礼策划"]) {
//            
//        }
    NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~~~~%@",self.myButtonName);
    
    [self.myCell.myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 回到顶部button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(300*kWidth/375, kHeight -  130 * kHeight / 667, 35*kWidth/375, 38*kHeight/667);
    [button setBackgroundImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
    button.alpha = 0.6;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(backTop:) forControlEvents:UIControlEventTouchUpInside];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(300 * kWidth / 375, 450 * kHeight / 667, 35 * kWidth / 375, 38 * kHeight / 667);
//    [button setBackgroundImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
//    button.alpha = 0.6;
//    [self.view addSubview:button];
//    [button addTarget:self action:@selector(backTop:) forControlEvents:UIControlEventTouchUpInside];
    self.myPage = 1;
//    [self setupRefresh];

    //上下拉刷新
    
    //网络请求
    [self addHeader];
    [self addFooter];
}
//
- (void)addHeader
{
    __block TaoCanViewController *taocanVC = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        taocanVC.myPage = 0;
        taocanVC.Loading = NO;//标记为下拉操作
        [self reCreateData];//重新请求数据
        
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    __block TaoCanViewController *taocanVC = self;
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        taocanVC.Loading = YES;//标记为上拉操作
        [taocanVC reCreateData];//请求更多数据
        
    }];
}
-(void)reCreateData

{
    self.myPage++;
    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",(long)self.myPage]];
        //      数据解析
        [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
            //      数据存入TaoCanAnLiModel
    
            NSMutableArray *array = result[@"works"];
    
            //        self.model1Array = [NSMutableArray array];
            for (NSDictionary *tempDic in array) {
                TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc] init];
                //                根据字典，给model各个属性赋值
                [mod setValuesForKeysWithDictionary:tempDic];
                [self.model1Array addObject:mod];
                [mod release];
            }
            //            网络数据获取完成，隐藏大风车
            [self.HUD hide:YES];
            //            刷新tableView
            [self.myTableView reloadData];
        }
         //        网络数据获取失败
                 failure:^(NSError *error) {
                     //      显示提示框
                     UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                     [alertV show];
                 }];
    
    [self.myTableView headerEndRefreshing];
    [self.myTableView footerEndRefreshing];
    [self.myTableView reloadData];
    
    
    
}
-(void)backTop:(UIButton *)button{
    [self.myTableView setContentOffset:CGPointZero animated:YES];
}

-(void)buttonAction:(UIButton *)button{


}
//用setter方法给套餐视图控制器传入上个页面点击的按钮名称，并从调用网络获取数据的方法
- (void)setMyButtonName:(NSString *)myButtonName
{
    if (_myButtonName != myButtonName) {
        [myButtonName release];
        _myButtonName = [myButtonName retain];
    }
    self.navigationController.title = self.myButtonName;
//    添加大风车
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUD setDimBackground:YES];
    self.HUD.labelText = @"Loading";
    [self.HUD show:YES];
    [self getData];
    
}
//从网络获取数据方法
-(void)getData{
   
        NSString *urlString = @"";
    
        
//    判断按钮名称，根据名称获取该名称对应的数据，
        if ([self.myButtonName isEqualToString:@"婚礼策划"]) {
//            获取套餐对应的网络数据
            urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:0]) objectAtIndex:0]) objectForKey:@"套餐"];
            NSLog(@"2222222222222%@",urlString);
            self.strUrl = urlString;
        }
        if ([self.myButtonName isEqualToString:@"婚纱摄影"]) {
            urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:1]) objectAtIndex:0]) objectForKey:@"套餐"];
            self.strUrl = urlString;
        }
        if ([self.myButtonName isEqualToString:@"摄影师"]) {
            urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:2]) objectAtIndex:0]) objectForKey:@"套餐"];
            self.strUrl = urlString;
        }
        if ([self.myButtonName isEqualToString:@"摄像师"]) {
            urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:3]) objectAtIndex:0]) objectForKey:@"套餐"];
            self.strUrl = urlString;
        }
        if ([self.myButtonName isEqualToString:@"化妆师"]) {
            urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:4]) objectAtIndex:0]) objectForKey:@"套餐"];
            self.strUrl = urlString;
        }
        if ([self.myButtonName isEqualToString:@"主持人"]) {
            urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:5]) objectAtIndex:0]) objectForKey:@"套餐"];
            self.strUrl = urlString;
        }
        
//      数据解析
        [NetworkTool get:urlString body:BodyHTTP httpResult:JSON success:^(id result) {
//      数据存入TaoCanAnLiModel
            
            NSMutableArray *array = result[@"works"];
            
            self.model1Array = [NSMutableArray array];
            for (NSDictionary *tempDic in array) {
                TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc] init];
//                根据字典，给model各个属性赋值
                [mod setValuesForKeysWithDictionary:tempDic];
                [self.model1Array addObject:mod];
                [mod release];
            }
//            网络数据获取完成，隐藏大风车
            [self.HUD hide:YES];
//            刷新tableView
            [self.myTableView reloadData];
        }
//        网络数据获取失败
        failure:^(NSError *error) {
//      显示提示框
            UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertV show];
        }];
        
        
}
//section
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    
//    return self.myButtonName;
//    
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    
//   打开数据库
    [[TaoCanAnLiDataBaseHandle shareDataBase]openDB];
//    创建表
    
    [[TaoCanAnLiDataBaseHandle shareDataBase]createTable];
//    获取当前内容
    TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc]init];
    mod = [self.model1Array objectAtIndex:indexPath.row];
    mod.name = [mod.merchant objectForKey:@"name"];
//   内容 插入表中
    [[TaoCanAnLiDataBaseHandle shareDataBase]insert:mod];
    
    
    
    

    
}
//// 刷新
//- (void)setupRefresh
//{
//    // 下拉刷新
//    [self.myTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    [self.myTableView headerBeginRefreshing];
//    
//    // 上拉加载
//    [self.myTableView addFooterWithTarget:self action:@selector(footerRereshing)];
//    //    [self.myTableView footerBeginRefreshing];
//}
//// 下拉
//- (void)headerRereshing
//{
//    //    [self.goodsCollectionView reloadData];
//    // 刷新结束
//    [self.myTableView headerEndRefreshing];
//}
//// 上拉
//- (void)footerRereshing
//{
//    self.myPage++;
//    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",self.myPage]];
//    //      数据解析
//    [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
//        //      数据存入TaoCanAnLiModel
//        
//        NSMutableArray *array = result[@"works"];
//        
//        //        self.model1Array = [NSMutableArray array];
//        for (NSDictionary *tempDic in array) {
//            TaoCanAnLiModel *mod = [[TaoCanAnLiModel alloc] init];
//            //                根据字典，给model各个属性赋值
//            [mod setValuesForKeysWithDictionary:tempDic];
//            [self.model1Array addObject:mod];
//            [mod release];
//        }
//        //            网络数据获取完成，隐藏大风车
//        [self.HUD hide:YES];
//        //            刷新tableView
//        [self.myTableView reloadData];
//    }
//     //        网络数据获取失败
//             failure:^(NSError *error) {
//                 //      显示提示框
//                 UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                 [alertV show];
//             }];
//    [self.myTableView reloadData];
//    [self.myTableView footerEndRefreshing];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
